package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.City;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.service.CityService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;

public class AdminUser extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminUser.class);
	
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	private List isShowList = new ArrayList();
	private List validList = new ArrayList();

	
	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	public List getValidList() {
		return validList;
	}

	public void setValidList(List validList) {
		this.validList = validList;
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void setValidListValues(){
		validList.add(new OptionsInt(-1, "请选择"));
		validList.add(new OptionsInt(0, "无效"));
		validList.add(new OptionsInt(1, "有效"));
	}
	
	private void initValues(){
		setIsShowListValues();
		setValidListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		setValidListValues();
	}


	private long id;					//主键ID
	private String userName;			//用户名称
	private int valid = -1;				//是否有效，0无效1有效
	private int isShow = -1;			//是否显示
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getValid() {
		return valid;
	}

	public void setValid(int valid) {
		this.valid = valid;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}


	private PageList pageList;
	private User user;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_user.action?userName="+this.userName+"&valid="+this.valid+"&isShow=" + this.isShow);
		this.setPageList(this.getUserService().findUsersByUserNameValidIsShow(this.userName, this.valid, this.isShow, "id", 1, pages));
		return SUCCESS;
	}
	
	public String show(){
		this.user = this.getUserService().findUserById(this.id);
		return "show";
	}
	
	public String updatevalid(){
		this.user = this.getUserService().findUserById(this.id);
		int valid = user.getValid();
		valid = Math.abs(valid-1);
		user.setValid(valid);
		this.getUserService().updateUser(user);
		return "index";
	}
	
	public String updateshow(){
		this.user = this.getUserService().findUserById(this.id);
		int isShow = user.getIsShow();
		isShow = Math.abs(isShow-1);
		user.setIsShow(isShow);
		this.getUserService().updateUser(user);
		return "index";
	}

}
